package cn.jia.hrm.service.impl;

import cn.jia.hrm.AjaxResult;
import cn.jia.hrm.PageList;
import cn.jia.hrm.client.LoginUserClient;
import cn.jia.hrm.domain.Employee;
import cn.jia.hrm.domain.LoginUser;
import cn.jia.hrm.domain.Tenant;
import cn.jia.hrm.domain.TenantType;
import cn.jia.hrm.exception.AssertGlobal;
import cn.jia.hrm.exception.GlobalException;
import cn.jia.hrm.mapper.EmployeeMapper;
import cn.jia.hrm.mapper.TenantMapper;
import cn.jia.hrm.query.TenantQuery;
import cn.jia.hrm.service.ITenantService;
import cn.jia.hrm.vo.TenantEnteringVo;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jia
 * @since 2021-09-11
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public PageList<Tenant> selectPagelist(TenantQuery query) {
        Page<TenantType> page = new Page<TenantType>(query.getPage(),query.getRows());
        List<Tenant> datas=tenantMapper.loadpagelist(query,page);
        return new PageList<>(page.getTotal(),datas);
    }
    @Autowired
    private LoginUserClient loginUserClient;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void entering(TenantEnteringVo vo) {

       /* Assert.hasText();*/
      /*  AssertGlobal.hasText(vo.getTenant().getCompanyName(),"公司的名字不能为空");
        AssertGlobal.hasText(vo.getTenant().getCompanyNum(),"公司的编号不能为空");
        AssertGlobal.isEmail(vo.getEmployee().getEmail(),"帅哥邮箱不能为空哦");
        AssertGlobal.isMobile(vo.getEmployee().getTel(),"请输入正确的手机号");*/

       /* if (StringUtils.isEmpty(vo.getTenant().getCompanyName())){
            throw  new GlobalException("失败了");
        }*/

        Employee employee = vo.getEmployee();
        Tenant tenant = vo.getTenant();
        //保存登录用户  并返回id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(employee.getUsername());
        loginUser.setPassword(employee.getPassword());
        loginUser.setType(0);
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);//调价用户并返回一个id
        Long id = Long.valueOf(ajaxResult.getResultObj().toString()); //获取返回的id
        employee.setLoginId(id);  //将userlogin的id赋值给 employee
        employeeMapper.insert(employee); // 将管理员的添加之数据库
        tenant.setAdminId(employee.getId());
        tenantMapper.insert(tenant);   //将租户保存到数据库中

        Long tenantId = tenant.getId();
        Long mealId = vo.getMealId();
        tenantMapper.addTenanMeal(mealId,tenantId);


    }
}
